package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UploadUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ServletContext ctx;

    @RequiresPermissions(value = {"employee:view", "员工查看"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "employee/employee";
    }

    @RequiresPermissions(value = {"employee:query", "员工列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(EmployeeQueryObject qo) {
        return employeeService.quey4List(qo);
    }

    @RequiresPermissions(value = {"class:selectAll", "班主任全选"}, logical = Logical.OR)
    @RequestMapping("selectAllClassManager")
    @ResponseBody
    public List<Employee> selectAllClassManager() {
        return employeeService.selectAllClassManager();
    }

    @RequiresPermissions(value = {"teacher:selectAll", "任课老师全选"}, logical = Logical.OR)
    @RequestMapping("selectAllTeacher")
    @ResponseBody
    public List<Employee> selectAllTeacher() {
        return employeeService.selectAllTeacher();
    }

    @RequiresPermissions(value = {"salesman:selectAllSalesman", "营销人员全选"}, logical = Logical.OR)
    @RequestMapping("selectAllSalesman")
    @ResponseBody
    public List<Employee> selectAllSalesman() {
        return employeeService.selectAllSalesman();
    }

    @RequiresPermissions(value = {"incomeMan:selectAllIncomMan", "收款人全选"}, logical = Logical.OR)
    @RequestMapping("selectAllIncomMan")
    @ResponseBody
    public List<Employee> selectAllIncomMan() {
        return employeeService.selectAllIncomMan();
    }

    @RequiresPermissions(value = {"employee:listAll", "全部员工"}, logical = Logical.OR)
    @RequestMapping("listAll")
    @ResponseBody
    public List<Employee> listAll() {
        return employeeService.selectAll();
    }


    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工新增/编辑"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee entity, MultipartFile pic) {
        try {
            if (StringUtils.hasLength(entity.getHead())) {
                UploadUtil.deleteFile(ctx, entity.getHead());
            }
            if (pic != null) {
                String dir = ctx.getRealPath("/static/upload/head");
                String head = UploadUtil.upload(pic, dir);
                entity.setHead(head);
                System.out.println(entity);
            }
            employeeService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"employee:changState", "员工离职/复职"}, logical = Logical.OR)
    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id) {
        try {
            employeeService.changState(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value = {"employee:resetpassword", "员工重置密码"}, logical = Logical.OR)
    @RequestMapping("resetpassword")
    @ResponseBody
    public JsonResult resetpassword(Employee entity) {
        try {
            employeeService.resetpassword(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response, EmployeeQueryObject qo) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");

        Workbook book = employeeService.exportXls(qo);

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }


    @RequestMapping("importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file, HttpServletResponse response) throws Exception {
        try {
            employeeService.importXls(file, response);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }


    /**
     * 获取文件上传模板
     *
     * @param response
     * @throws Exception
     */
    @RequestMapping("getImportXlsMode")
    public void importXlsMode(HttpServletResponse response) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");

        Workbook book = employeeService.getImportXlsMode();

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }

    /**
     * 通过角色来获取员工集合
     *
     * @param roleSn
     * @return
     */
    @RequestMapping("selectEmpByRolesn")
    @ResponseBody
    public List<Employee> selectEmpByRolesn(String roleSn) {
        return employeeService.selectEmpByRolesn(roleSn);

    }
}
